﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Windows.Forms;

namespace RoketPack
{
    class UNC
    {
        /// <summary>
        /// Returns the correct UNC path for the path specified (resolves mapped drive names).
        /// </summary>
        /// <remarks>
        /// Sourced from http://groups.google.nu/group/dotnetdevelopment/browse_thread/thread/8c37da48433fbf9c/6caffd33ad1eae11.
        /// </remarks>
        /// <param name="path">The path to resolve.</param>
        /// <returns>The proper UNC path.</returns>
        public static string GetUNCPath(string path)
        {
            if (path == string.Empty || path.IndexOf(":") > 1) 
                return path; 
            if (path.StartsWith("\\"))
                return (new Uri(path)).ToString();

            ManagementObjectSearcher searcher = new 
            ManagementObjectSearcher("SELECT RemoteName FROM win32_NetworkConnection WHERE LocalName = '" + path.Substring(0, 2) + "'"); 
            foreach (ManagementObject managementObject in searcher.Get())
            {
                string name = managementObject["RemoteName"] as string;
                name += path.Substring(2);
                return (new Uri(name)).ToString();
            }
            return path;
        }

        public static string UniversalApplicationPath
        {
            get
            {
                return UNC.GetUNCPath(Application.ExecutablePath);
            }
        }
    }
}
